unit kernelSettingsForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, PBCheckLB, Globals, KernelUnit;

type
  TkernelSettingsScreen = class(TForm)
    Panel2: TPanel;
    Panel1: TPanel;
    Button2: TButton;
    sdLabel1: TLabel;
    NameEdit: TEdit;
    Label1: TLabel;
    allTexturesList: TPBCheckListBox;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Label2: TLabel;
    inputTexturesList: TPBCheckListBox;
    Label3: TLabel;
    outputTexturesList: TPBCheckListBox;
    Button5: TButton;
    Button6: TButton;
    Label4: TLabel;
    allcBuffersList: TPBCheckListBox;
    Button7: TButton;
    Button8: TButton;
    Label5: TLabel;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    SaveDialog1: TSaveDialog;
    cBuffersLista: TPBCheckListBox;
    procedure Button1Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
  private
    { Private declarations }
    f : textFile;

    procedure clearGUI();
  public
    { Public declarations }
  end;

var
  kernelSettingsScreen: TkernelSettingsScreen;

implementation

{$R *.dfm}

procedure TkernelSettingsScreen.Button1Click(Sender: TObject);
var
  n : String;
begin
SaveDialog1.FilterIndex := 2;//TEXTURE_MODE - 1000;
if SaveDialog1.Execute then
  begin
    allTexturesList.Clear;
    assignFile(f, SaveDialog1.FileName);
    Reset(f);
    while not EOF(f) do
      begin
        Readln(f, n); // read the name
        allTexturesList.Items.Add(n);
        // skip all other data
        Readln(f, n); Readln(f, n); Readln(f, n); Readln(f, n);
        Readln(f, n); Readln(f, n); Readln(f, n); Readln(f, n);
      end;
    closeFile(f);
  end;
end;

procedure TkernelSettingsScreen.Button7Click(Sender: TObject);
var
  n : String;
begin
SaveDialog1.FilterIndex := 1;//TEXTURE_MODE - 1000;
if SaveDialog1.Execute then
  begin
    allcBuffersList.clear;
    assignFile(f, SaveDialog1.FileName);
    Reset(f);
    while not EOF(f) do
      begin
        Readln(f, n); // read the name
        allcBuffersList.Items.Add(n);
        // skip all other data
        Readln(f, n); Readln(f, n); Readln(f, n); Readln(f, n);
      end;
    closeFile(f);
  end;
end;

procedure TkernelSettingsScreen.Button11Click(Sender: TObject);
begin
close();
end;

procedure TkernelSettingsScreen.Button3Click(Sender: TObject);
var
  i : Integer;
begin
  for i:= 0 to allTexturesList.Count-1 do
    begin
      if allTexturesList.State[i] = cbchecked then
         begin
           inputTexturesList.Items.Add(allTexturesList.Items.Strings[i]);
           allTexturesList.State[i] := cbUnchecked; 
         end;
    end;
end;

procedure TkernelSettingsScreen.Button4Click(Sender: TObject);
var
  i : Integer;
begin
  for i:= 0 to allTexturesList.Count-1 do
    begin
      if allTexturesList.State[i] = cbchecked then
         begin
           outputTexturesList.Items.Add(allTexturesList.Items.Strings[i]);
           allTexturesList.State[i] := cbUnchecked;
         end;
    end;
end;

procedure TkernelSettingsScreen.Button8Click(Sender: TObject);
var
  i : Integer;
begin
  for i:= 0 to allcBuffersList.Count-1 do
    begin
      if allcBuffersList.State[i] = cbchecked then
         begin
           cBuffersLista.Items.Add(allcBuffersList.Items.Strings[i]);
           allcBuffersList.State[i] := cbUnchecked;
         end;
    end;
end;

procedure TkernelSettingsScreen.Button5Click(Sender: TObject);
begin
removeItem(inputTexturesList, 0);
end;

procedure TkernelSettingsScreen.Button6Click(Sender: TObject);
begin
removeItem(outputTexturesList, 0);
end;

procedure TkernelSettingsScreen.Button9Click(Sender: TObject);
begin
removeItem(cBuffersLista, 0);
end;

procedure TkernelSettingsScreen.Button2Click(Sender: TObject);
var
  Ker   : KernelClass;
  i : Integer;
begin
  if outputTexturesList.Count > 0 then
  begin
    if NameEdit.Text <> '' then
    begin
      Ker := KernelClass.create(NameEdit.Text);
      for i:=0 to inputTexturesList.Count-1 do
        Ker.add_InputTexture(inputTexturesList.Items.Strings[i]);
      for i:=0 to outputTexturesList.Count-1 do
        Ker.add_OutputTexture(outputTexturesList.Items.Strings[i]);
      for i:=0 to cBuffersList.Count-1 do
        Ker.add_ConstantBuffer(cBuffersLista.Items.Strings[i]);
      KernelList.Add(Ker);
      clearGUI();
    end
    else showMessage('You have to name this kernel!');
  end
  else
    showMessage('You have to enter one or more output textures!');
end;

procedure TkernelSettingsScreen.Button10Click(Sender: TObject);
var
 i, j, size : Integer;
 k : KernelClass;
begin
SaveDialog1.FilterIndex := 3;
if SaveDialog1.Execute then
   begin
      AssignFile(f, SaveDialog1.FileName);
      if not FileExists(SaveDialog1.FileName) then
         rewrite(f)
      else Append(f);
      for i:=0 to KernelList.Count-1 do
        begin
          k := KernelClass(KernelList.Items[i]);
          Writeln(f, k.get_Name());

          size := k.get_InputTexturesListSize();
          Writeln(f, size); // write the size of input textures
          for j:=0 to size-1 do
            Writeln(f, k.get_InputTexture(j));

          size := k.get_OutputTexturesListSize();
          Writeln(f, size); // write the size of output textures
          for j:=0 to size-1 do
            Writeln(f, k.get_OutputTexture(j));

          Writeln(f, '*****************************'); // seperator
        end;
      closeFile(f);
      KernelList.Clear;
   end;
end;

procedure TkernelSettingsScreen.clearGUI();
begin
NameEdit.Text := '';
//allTexturesList.Clear;
inputTexturesList.Clear;
outputTexturesList.Clear;
//allcBuffersList.Clear;
cBuffersLista.Clear;
ActiveControl := NameEdit;
end;

end.
